<?php
namespace app\index\controller;

use think\Db;                       // db
use think\Session;
use app\common\controller\HomeBase;  // model
use app\common\model\Wacategory as WacategoryModel;
use app\common\model\Wenan as WenanModel;

class Wenan extends HomeBase
{
    protected $wacategory_model;
    protected $wenan_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wacategory_model = new WacategoryModel();
        $this->wenan_model = new WenanModel();
        $category = $this->wacategory_model->where('pid',0)->select();
        $this->assign('category',$category);
        // $category_level_list = $this->wacategory_model->getLevelList();
        // $erji = $this->wacategory_model->where('pid','neq','0')->select();
        // $this->assign('erjicategory_level_list', $erji);
        // $this->assign('category_level_list',$category_level_list);
    }

    public function docs(){
        // 根据分类筛选显示文案  显示  wenan 里面的所有数据 和 分类数据里面的分类名称分开
        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
        $this->assign('wenan_list',$result);
        $this->assign('title','文案列表');
        return $this->fetch();
    }

    public function index()
    {
        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
        $this->assign('wenan_list',$result);
        $this->assign('title','文案列表');
        return $this->fetch();
    }

    public function i( $keyword = '', $page = 1){
        $map   = [];
        $field = 'id,title,wid,author,reading,status,publish_time,sort';

        // 筛选分类
        if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }

        $result = $this->wenan_model->field($field)->where($map)->order(['publish_time' => 'DESC'])->paginate(15, false, ['page' => $page]);

        $wecategory = $this->wacategory_model->column('name', 'id');

//        $result = $this->wenan_model->alias('w')->field('w.*,think_wacategory.name')->where('status',1)->join('think_wacategory','w.wid = think_wacategory.id')->order('w.sort desc')->select();
//        $this->assign('wenan_list',$result);
//        $this->assign('title','文案列表');
        return $this->fetch('i', ['wecategory_list' => $wecategory, 'wenan_list' => $result, 'keyword' => $keyword,'title','文案列表']);
    }

    public function wenan_detail($id){

        $index_id = session::get('index_id');
        
        if($tiao_list = $this->wacategory_model->where('pid',$id)->select()){
            $this->assign('tiao_list',$tiao_list);
            $this->assign('title','一跳三跳');
            return $this->fetch('tiao_detail');
        }
        if($index_id){
            $result = $this->wenan_model->where('wid',$id)->select();
        }else{
            $result = $this->wenan_model->where('wid',$id)->limit(0,10)->select();
        }
        $this->assign('title','文案列表');
        $this->assign('wenan_list',$result);
        return $this->fetch('wenan_detail');
    }


    /**
     * 标题页
     */
    public function wenantitle($id = '' , $sort = 0 , $type = 0 ,$page = 1){
        if(!empty($id)){
            $this->wacategory_model->where('id',$id)->setInc('click');
        }else{
            $id = 1;
        }
        
        if ($sort == 0) {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }else if($sort == 1){
            $order = 'create_time DESC';
        } else if ($sort == 2) {
            $order = 'downloadding DESC,readding DESC';
        } 
        // else if ($sort == 3) {
        //     $order = 'readding DESC';
        // }
         else {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }
        // $article_list = $this->wenan_model->query("SELECT * FROM `think_wenan` WHERE `type` = {$type} AND  `wid` IN (SELECT id FROM think_wacategory WHERE pid = {$id}) ORDER BY `id` DESC")->paginate(2,false,['page' => $page]);
        $ids = $this->wacategory_model->where('pid',$id)->column('id');
        $article_list = $this->wenan_model->where('type',$type)->whereIn('wid',$ids)->order($order)->paginate(20,false,['page' => $page]);
        $this->assign('article_list',$article_list);
        $this->assign('type',$type);
        $this->assign('sort',$sort);
        $this->assign('id',$id);
        $this->assign('nav',1);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 视频列表
     */
    public function video($id = '' , $sort = 0 , $type = 0 ,$page = 1){
        if(!empty($id)){
            $this->wacategory_model->where('id',$id)->setInc('click');
        }else{
            $id = 1;
        }
        
        if ($sort == 0) {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }else if($sort == 1){
            $order = 'create_time DESC';
        } else if ($sort == 2) {
            $order = 'downloadding DESC,readding DESC';
        } 
        // else if ($sort == 3) {
        //     $order = 'readding DESC';
        // }
         else {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }
        // $article_list = $this->wenan_model->query("SELECT * FROM `think_wenan` WHERE `type` = {$type} AND  `wid` IN (SELECT id FROM think_wacategory WHERE pid = {$id}) ORDER BY `id` DESC")->paginate(2,false,['page' => $page]);
        $ids = $this->wacategory_model->where('pid',$id)->column('id');
        $article_list = $this->wenan_model->where('type',$type)->whereIn('wid',$ids)->order($order)->paginate(20,false,['page' => $page]);
        $this->assign('article_list',$article_list);
        $this->assign('type',$type);
        $this->assign('sort',$sort);
        $this->assign('id',$id);
        $this->assign('nav',1);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /**
     * 标题页详情
     */
    public function wenantitledetail(){
        return $this->fetch();
    }

    /**
     * 视频首页
     */
    public function videoindex(){
        $this->assign('nav',0);
        return $this->fetch();
    }
}

/*
 * CREATE TABLE `think_wenan` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `wid` int(11) NOT NULL,
  `wenan_path` varchar(255) NOT NULL,
  `compress` varchar(255) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `thumb` varchar(255) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `sort` int(11) DEFAULT '0',
  `create_time` datetime NOT NULL,
  `status` tinyint(1) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8;
*/






